"""
Case Type   : rowcount_big()函数
Case Name   : D库无插件使用rowcount_big(),合理报错
Create At   : 2025/07/30
Owner       : zym_2025
Description :
    1、创建D库;
    2、D库中建表;
    3、执行rowcount_big();
    4、清理环境;
Expect      :
    1、建库成功;
    2、建表成功
    3、报错;
    4、清理环境成功;
History     :
"""

import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

class RowcountBig(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info('--OpenGauss_Compatibility_Function_Rowcount_Big_Case0027 start--')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_d'
        self.table_name = 't_rowcount_0027'

    def testRowcountBig(self):
        text = '------step1:创建D库;expect:成功------'
        self.log.info(text)
        create_db = f"drop database if exists {self.db_name};create database {self.db_name} dbcompatibility 'D';"
        res = self.comsh.execut_db_sql(create_db)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, '创建D库失败')

        text = '------step2:D库中建表;expect:成功------'
        self.log.info(text)
        create_sql = f"drop table if exists {self.table_name}; create table {self.table_name} (a int);"
        res = self.comsh.execut_db_sql(create_sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res, '建表失败' + text)

        text = '------step3:执行rowcount_big();expect:失败------'
        self.log.info(text)
        sql = "select rowcount_big();"
        res = self.comsh.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn('ERROR', res, 'rowcount_big()返回结果异常')

    def tearDown(self):
        text = '------step4:清理环境; expect:成功------'
        self.log.info(text)
        del_sql = f"drop table if exists {self.table_name};"
        res = self.comsh.execut_db_sql(del_sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')

        drop_sql = f"drop database {self.db_name};"
        res = self.comsh.execut_db_sql(drop_sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删除D库失败')
        self.log.info(f'------OpenGauss_Compatibility_Function_Rowcount_Big_Case0027 end------')